package com.shundr.common.http;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.common.model.VersionInfo;
import com.shundr.frame.constant.LCommands;
import com.shundr.frame.http.HttpResult;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.frame.http.LHttpRequest.RequestCompleteListener;

public class CommonRequest extends LHttpRequest implements
		RequestCompleteListener {
	public static final int GET_APPSYS_INFO_UPDATE = 110;// 需要更新
	public static final int GET_APPSYS_INFO_NO_UPDATE = 12;// 不需要更新
	public static final int GET_APPSYS_INFO_FAILED = 13;// 数据库更新，请稍后
	public static final int CHECK_LOGIN_STATUS_SUCCEED=14;
	public static final int SAVE_APP_OPERATE_SUCCEED=15;
	public static final int GIVE_ADVICE_SUCCEED=16;
	public static final int SAVE_OPERATE_HISTORY_SUCCESSED = 17;//保存操作历史
	
	public static final int ADD_FAVORITE_SUCCEED=18;//添加收藏
	public static final int GET_FAVORITE_LIST_SUCCEED=19;//获取收藏列表
	public static final int DELETE_FAVORITE_SUCCEED=20;//取消收藏
	
	public static final int GET_HISTORY_LIST_SUCCEED=21;//获取历史列表
	public static final int UPLOAD_LOCATION_SUCCEED=22;//上传实时地理位置成功
	
	public static final int GET_SYSTEM_MSG_LIST_SUCCEED=23;//获取系统消息列表成功
	public static final int GET_RECOM_CARGO_LIST_SUCCEED=24;//获取推荐货源列表成功
	
	public static final int GET_SPLASH_IMAGE_SUCCESSED = 201;//

	private Handler mHandler;
	private Message mMessage;
	private Context mContext;

	private Map<String, Object> param;
	private String command;

	public CommonRequest(Context context, Handler handler) {
		this.mContext = context;
		this.mHandler = handler;
		this.mMessage = mHandler.obtainMessage();
		this.setRequestCompleteListener(this);
	}

	/**
	 * 获取用户信息
	 */
	public void getAppSysInfo(String appId, String mobileId) {
		param = new HashMap<String, Object>();
		param.put("appId", appId);
		param.put("mobileId", mobileId);
		param.put("appType", 1);
		command = LCommands.GET_APPSYS_INFO;
		requestByGet(param);
	}

	/**
	 * 检查登录状态
	 */
	public void checkLoginStatus() {
		param = new HashMap<String, Object>();
		command = LCommands.CHECK_LOGIN_STATUS;
		requestByPost(param);
	}

	/**
	 * 保存用户操作
	 * 
	 * @param operateType
	 */
	public void saveAppOperate(int operateType) {
		param = new HashMap<String, Object>();
		param.put("operateType", operateType);
		command = LCommands.SAVE_APP_OPERATE;
		requestByPost(param);
	}
	
	/**
	 * 保存操作历史
	 * saveOperateHistory(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param id
	 * @param objectType 0货源,1车源,2货主,3车主
	 * @param operateType 0查阅,1打电话
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void saveOperateHistory(int id, int objectType,
			int operateType,String title) {
		param = new HashMap<String, Object>();
		param.put("operateObjectId", id);
		param.put("operateObjectType", objectType);
		param.put("operateType", operateType);
		param.put("title", title);
		command = LCommands.SAVE_OPERATE_HISTORY;
		requestByPost(param);
	}

	/**
	 * 意见反馈
	 * @param adviceDes 建议描述
	 * @param advicePhone 联系方式
	 */
	public void giveAdvice(String adviceDes,String advicePhone) {
		param = new HashMap<String, Object>();
		param.put("adviceDes", adviceDes);
		param.put("advicePhone", advicePhone);
		command = LCommands.GIVE_ADVICE;
		requestByPost(param);
	}
	
	/**
	 * 获取浏览历史
	 * getHistoryList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param operateType
	 * @param pageIndex
	 * @param pageSize
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getHistoryList(int operateType, int pageIndex, int pageSize) {
		param = new HashMap<String, Object>();
		param.put("operateType", operateType);
		param.put("pageIndex", pageIndex);
		param.put("pageSize", pageSize);
		command = LCommands.GET_HISTORY_LIST;
		requestByGet(param);
	}
	
	/**
	 * 添加收藏
	 * addFavorite(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param objectId
	 * @param objectType
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void addFavorite(int objectId, int objectType) {
		param = new HashMap<String, Object>();
		param.put("objectId", objectId);
		param.put("objectType", objectType);
		command = LCommands.ADD_FAVORITE;
		requestByGet(param);
	}
	
	/**
	 * 获取收藏列表
	 * getFavoriteList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param pageIndex
	 * @param pageSize
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getFavoriteList(int pageIndex, int pageSize) {
		param = new HashMap<String, Object>();
		param.put("pageIndex", pageIndex);
		param.put("pageSize", pageSize);
		command = LCommands.GET_FAVORITE_LIST;
		requestByGet(param);
	}
	
	/**
	 * 取消收藏
	 * deleteFavorite(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param objectId
	 * @param objectType
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void deleteFavorite(int objectId, int objectType) {
		param = new HashMap<String, Object>();
		param.put("objectId", objectId);
		param.put("objectType", objectType);
		command = LCommands.DELETE_FAVORITE;
		requestByGet(param);
	}
	
	/**
	 * 上传地理位置
	 * uploadLocation(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param Lat
	 * @param Lon
	 * @param address
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void uploadLocation(double Lat,double Lon,String address){
		param = new HashMap<String, Object>();
		param.put("locationLng", Lon);
		param.put("locationLat", Lat);
		param.put("city", address);
		command = LCommands.UPLOAD_LOCATION;
		requestByPost(param);
	}
	
	/**
	 * 
	 * getSystemList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param pageIndex
	 * @param pageSize
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getSystemMsgList(int pageIndex, int pageSize) {
		param = new HashMap<String, Object>();
		param.put("pageIndex", pageIndex);
		param.put("pageSize", pageSize);
		command = LCommands.GET_SYSTEM_MSG_LIST;
		requestByGet(param);
	}
	
	/**
	 * 推荐货源列表
	 * getRecommendList(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param pageIndex
	 * @param pageSize
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getRecommendList(int pageIndex, int pageSize) {
		param = new HashMap<String, Object>();
		param.put("pageIndex", pageIndex);
		param.put("pageSize", pageSize);
		command = LCommands.GET_RECOM_CARGO_LIST;
		requestByGet(param);
	}
	
	/**
	 * 获取启动页
	 */
	public void syncSplashImage(String time) {
		param = new HashMap<String, Object>();
		param.put("time", time);
		command = LCommands.GET_SPLASH_IMAGE;
		requestByGet(param);
	}
	
	/**
	 * 将异常日志发送到服务器
	 * sendCrashLog(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param logMessage
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void sendCrashLog(String logMessage) {
		param = new HashMap<String, Object>();
		param.put("logMessage", logMessage);
		command = LCommands.SEND_CRASH_LOG;
		requestByPost(param);
	}

	@Override
	protected void initParams() {
		super.initParams();
		mParam = param;
		mCommand = command;
		mBaseContext = mContext;
	}

	@Override
	public void requestSuccessed(HttpResult result) {
		if(mHandler == null)
			return;
		String results = result.getResult();
		if (command.equals(LCommands.GET_APPSYS_INFO)) {
			VersionInfo info = CommonParser.parseVersionInfo(results);
			if (info != null) {
				mMessage.what = GET_APPSYS_INFO_UPDATE;
				mMessage.obj = info;
			} else {
				mMessage.what = GET_APPSYS_INFO_NO_UPDATE;
				mMessage.obj = result.getDescription();
			}
		} else if(command.equals(LCommands.SAVE_OPERATE_HISTORY)){
			
		} else if(command.equals(LCommands.CHECK_LOGIN_STATUS)){
			mMessage.what = CHECK_LOGIN_STATUS_SUCCEED;
			mMessage.obj = result.getDescription();
		}else if(command.equals(LCommands.SAVE_APP_OPERATE)){
			mMessage.what = SAVE_APP_OPERATE_SUCCEED;
			mMessage.obj = result.getDescription();
		}else if(command.equals(LCommands.GIVE_ADVICE)){
			mMessage.what = GIVE_ADVICE_SUCCEED;
			mMessage.obj = result.getDescription();
		}else if(command.equals(LCommands.GET_HISTORY_LIST)){
			mMessage.what = GET_HISTORY_LIST_SUCCEED;
			//历史
			mMessage.obj = CommonParser.parserHistoryList(result.getResult());
		}else if(command.equals(LCommands.ADD_FAVORITE)){
			mMessage.what = ADD_FAVORITE_SUCCEED;
			mMessage.obj = result.getDescription();
		}else if(command.equals(LCommands.GET_FAVORITE_LIST)){
			mMessage.what = GET_FAVORITE_LIST_SUCCEED;
			//收藏
			mMessage.obj = CommonParser.parserFavoriteList(result.getResult());
		}else if(command.equals(LCommands.DELETE_FAVORITE)){
			mMessage.what = DELETE_FAVORITE_SUCCEED;
			mMessage.obj = result.getDescription();
		}else if(command.equals(LCommands.UPLOAD_LOCATION)){
			mMessage.what = UPLOAD_LOCATION_SUCCEED;
			mMessage.obj = result.getDescription();
		}else if(command.equals(LCommands.GET_SYSTEM_MSG_LIST)){
			mMessage.what = GET_SYSTEM_MSG_LIST_SUCCEED;
			mMessage.obj = CommonParser.parserSysList(result.getResult());
		}else if(command.equals(LCommands.GET_RECOM_CARGO_LIST)){
			mMessage.what = GET_RECOM_CARGO_LIST_SUCCEED;
			mMessage.obj = CommonParser.parserRecomCargoList(result.getResult());
		}else if (command.equals(LCommands.GET_SPLASH_IMAGE)) {
			mMessage.what = GET_SPLASH_IMAGE_SUCCESSED;
			mMessage.obj = CommonParser.parserSplash(result.getResult());
		}
		mHandler.sendMessage(mMessage);
	}

	@Override
	public void requestFailed(HttpResult result) {
		if(mHandler == null)
			return;
		// 请求失败,result的返回码可判断失败原因，进行如下操作
		mMessage.what = REQUEST_FAILED;
		mMessage.obj = result.getDescription();
		mHandler.sendMessage(mMessage);
	}
}
